﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Lilium.Control.Reactive;

namespace Lilium.UI.Forms
{
    //TODO: убрать интерфейс адаптера
	public sealed class TextBoxAdapter : ControlAdapter<TextBox>, IFieldAdapter, ITextField
	{
		public TextBoxAdapter(TextBox textBox)
            :base(textBox)
		{
			if (textBox == null) throw new ArgumentNullException("textBox");

            textBox.TextChanged += (object sender, EventArgs e) => OnPushed();
		}

		#region public bool Enabled

		public bool Enabled
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return Control.Enabled;
			}
			[DebuggerNonUserCodeAttribute]
			set
			{
                Control.Enabled = value;
			}
		}

		#endregion

		#region IFieldAdapter Members

		public object Value
		{
			get
			{
                return Control.Text;
			}
			set
			{
                Control.Text = string.Format("{0}", value);
			}
		}

		#endregion

        #region ISource<string> Members

        public event Action<string> Pushed;

        private void OnPushed()
        {
            var pushed = Pushed;
            if (pushed != null)
                pushed(Control.Text);
        }

        #endregion

        #region ITextField Members

        public string Text
        {
            get
            {
                return Control.Text;
            }
            set
            {
                Control.Text = value ;
            }
        }

        #endregion
    }
}
